package tt.dz.biz.service.financial;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.financial.SubjectDAOImpl;
import tt.dz.common.BusinessException;
import tt.dz.common.DzErrorCode;
import tt.dz.common.DzErrorCode.ExceptionErrorCode;
import tt.dz.common.LogUtil;
import tt.dz.vo.SubjectVO;

@Service("subjectService")
public class SubjectService
{
  @Resource(name="subjectDAO")
  private SubjectDAOImpl subjectDAOImpl;
  @Value("${accountingRuleSubject}")
  private String accountingRuleSubject;
  @Value("${assetsLbltsAccountRuleSubject}")
  private String assetsLbltsAccountRuleSubject;
  
  public String getAccountingRuleSubject()
  {
    return this.accountingRuleSubject;
  }
  
  public String getAssetsLbltsAccountRuleSubject()
  {
    return this.assetsLbltsAccountRuleSubject;
  }
  
  public List<SubjectVO> queryChannelSubject()
  {
    List<SubjectVO> subjectList = null;
    try
    {
      subjectList = this.subjectDAOImpl.queryChannelSubject();
    }
    catch (Exception e)
    {
      LogUtil.error("查询可关联支付渠道的二级科目类型异常", e);
      subjectList = new ArrayList();
    }
    return subjectList;
  }
  
  public List<SubjectVO> querySubject()
  {
    List<SubjectVO> subjectList = null;
    try
    {
      subjectList = this.subjectDAOImpl.querySubject();
    }
    catch (Exception e)
    {
      LogUtil.error("查询所有的科目类型异常", e);
      subjectList = new ArrayList();
    }
    return subjectList;
  }
  
  public boolean insertSubject(SubjectVO subjectVO)
  {
    boolean excuteFlag = false;
    try
    {
      subjectVO.setSubjectCode(this.subjectDAOImpl.productSubjectCode(subjectVO).toString());
      int i = this.subjectDAOImpl.insertSubject(subjectVO);
      excuteFlag = i > 0;
      if ((excuteFlag) && (!"#".equals(subjectVO.getParentSubjectCode()))) {
        this.subjectDAOImpl.insertMerchantSubject(subjectVO);
      }
    }
    catch (Exception e)
    {
      LogUtil.error("插入科目类型异常--入参：" + subjectVO.toString(), e);
      
      throw new BusinessException(DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.value(), DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.getDesc(), e.getMessage());
    }
    return excuteFlag;
  }
  
  public boolean updateSubject(SubjectVO subjectVO)
  {
    boolean excuteFlag = false;
    try
    {
      int i = this.subjectDAOImpl.updateSubject(subjectVO);
      excuteFlag = i > 0;
    }
    catch (Exception e)
    {
      excuteFlag = false;
      LogUtil.error("更新科目类型异常--入参：" + subjectVO.toString(), e);
    }
    return excuteFlag;
  }
  
  public SubjectVO querySubjectByCode(String subjectCode)
    throws Exception
  {
    try
    {
      return this.subjectDAOImpl.querySubjectByCode(subjectCode);
    }
    catch (Exception e)
    {
      LogUtil.error("根据科目编码查询科目信息异常，参数为：subjectCode= " + subjectCode, e);
      throw e;
    }
  }
  
  public List<SubjectVO> querySubjectByNameAndParentCode(SubjectVO subjectVO)
    throws Exception
  {
    try
    {
      return this.subjectDAOImpl.querySubjectByNameAndParentCode(subjectVO);
    }
    catch (Exception e)
    {
      LogUtil.error("查询同级科目中同名的科目异常，参数为：subjectVO= " + subjectVO, e);
      throw e;
    }
  }
}
